package handler

import (
	c "dkai/config"
	m "dkai/model"
	"dkai/template"
	"fmt"
	"github.com/goccy/go-json"
	"io"
	"net/http"
	"strings"
)

func DkCallback(say string) {
	config := c.GlobalConfig
	fmt.Printf("钉钉回执地址为：%v\n", config.Bot.Webhook)

	templateSelected := template.Select(template.TempType(1), say)

	//markdown := m.MarkdownContent{
	//	Title: "DKAi收到主人回复",
	//	Text:  "我要说我要说！！！",
	//}
	callbackModel := m.DKAiCallbackModel{
		Msgtype:   "markdown",
		Content:   m.MarkdownContent(templateSelected),
		AtMobiles: nil,
		AtUserIds: nil,
		IsAtAll:   false,
	}

	marshal, _ := json.Marshal(callbackModel)

	resp, err := http.Post(config.Bot.Webhook, "application/json", strings.NewReader(string(marshal)))
	if err != nil {
		fmt.Printf("DKAi回执出错啦～ %v\n", err.Error())
	}
	defer func(Body io.ReadCloser) {
		_ = Body.Close()
	}(resp.Body)

	bodyBytes := resp.Body
	body, _ := io.ReadAll(bodyBytes)

	fmt.Printf("报告主人，DKAi收到结果：%v\n", string(body))
}
